package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.constant.LikeContext;
import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;
import com.honglv.mapper.AttractionsMapper;
import com.honglv.mapper.StrategyMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadAttractionsDTO;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.entity.Attractions;
import com.honglv.pojo.vo.GetAttractionsByCityVO;
import com.honglv.pojo.vo.GetAttractionsByCityVO;
import com.honglv.pojo.dto.GetAttractionDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateAcctractionDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllAttractionsVO;
import com.honglv.pojo.vo.GetAttractionVO;
import com.honglv.pojo.vo.GetTopAttractionVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.AttractionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AttractionsServiceImpl implements AttractionsService {
    @Autowired
    private AttractionsMapper attractionsMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * Description:获取景点详情页
     * date: 2024/03/31
     * author:王铭杰
     */
    public GetAttractionVO getAcctraction(GetAttractionDTO getAcctractionDTO) {
//        public static final String[] likeStatus = {"rounte","attractions","strategy","news"};
        GetAttractionVO getAttractionVO = attractionsMapper.getAcctraction(getAcctractionDTO.getId());
        if (getAttractionVO == null){
            return null;
        }
        String key = LikeContext.likeStatus[1] + getAcctractionDTO.getId();
        Long userId = BaseContext.getCurrentId();
        boolean identify = Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(key, String.valueOf(userId)));
        Integer likeStatus = identify? 1 : 0;
        getAttractionVO.setLikeStatus(likeStatus);
        return getAttractionVO;
    }

    /**
     * Description:分页获取景点
     * date: 2024/03/31
     * author:王铭杰
     */
    public PageVO<GetAllAttractionsVO> getAllAttractions(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        List<GetAllAttractionsVO> list = attractionsMapper.getAllAttractions();
        if (list == null){
            return null;
        }
        PageInfo<GetAllAttractionsVO> pageInfo = new PageInfo<>(list);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * Description:获取点赞量最高的9个景点
     * date: 2024/03/31
     * author:王铭杰
     */
    public List<GetTopAttractionVO> getTopAttraction() {
        List<GetTopAttractionVO> list = attractionsMapper.getTopAttraction();
        return list;
    }

    /**
     * Description:通过用户id获取景点
     * date: 2024/03/31
     * author:王铭杰
     */
    public List<GetTopAttractionVO> getAttractionsByAuthorId() {
        Long userId = BaseContext.getCurrentId();
        if (userId == null){
            throw new BaseException("用户未登录");
        }
        List<GetTopAttractionVO> list = attractionsMapper.getAttractionsByAuthorId(userId);
        return list;
    }

    @Override
    public Result deleteAcctraction(Long id) {
        attractionsMapper.deleteAcctraction(id);
        return Result.success("删除成功!");
    }

    @Override
    public Result updateAcctraction(UpdateAcctractionDTO updateAcctractionDTO) {
        if(attractionsMapper.updateAcctraction(updateAcctractionDTO) > 0){
            return Result.success("修改成功！");
        }
        return Result.success("修改失败，未知错误!");
    }
    /**
     * 管理员上传景点
     * @param
     * @return
     */
    @Override
    public void upload(UploadAttractionsDTO uploadAttractionsDTO) {
        Long authorId = BaseContext.getCurrentId();
        Attractions attractions=new Attractions();
        BeanUtils.copyProperties(uploadAttractionsDTO,attractions);
        attractions.setAuthorId(authorId);
        attractions.setLike(0L);
        attractions.setViews(0L);
        attractionsMapper.insert(attractions);


    }
    /**
     * 根据城市分页获取景点
     * @param
     * @return
     */

    @Override
    public PageVO<GetAttractionsByCityVO> getAttractionsByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Attractions> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Attractions> list = attractionsMapper.selectList(qw);
        GetAttractionsByCityVO gf=new GetAttractionsByCityVO();
        List<GetAttractionsByCityVO> attractions=new ArrayList<>();
        for(Attractions Attractions:list){
            BeanUtils.copyProperties(Attractions,gf);
            attractions.add(gf);
        }
        if (attractions == null){
            return null;
        }
        PageInfo<GetAttractionsByCityVO> pageInfo = new PageInfo<>(attractions);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
